package com.service;

import com.bean.GroupData;
import com.util.RowMapper;
import com.util.SqliteHelper;
import com.util.TimeUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class GroupService {

    public static Boolean save(GroupData groupData) {
        try {
            SqliteHelper h = new SqliteHelper();
            h.executeUpdate("insert into `group` values('" + groupData.getRole() + "','" + groupData.getWx() + "','" + groupData.getNum() + "','" + groupData.getTime() + "');");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     *
     * @return
     */
    public static List<GroupData> getList() {
        try {
            SqliteHelper h = new SqliteHelper();
            List<GroupData> sList = h.executeQuery("select * from `group` a WHERE time IN (SELECT time FROM  `group` b WHERE a.wx=b.wx ORDER BY time desc limit 1 )  GROUP BY wx ", new RowMapper<GroupData>() {
                @Override
                public GroupData mapRow(ResultSet rs, int index)
                        throws SQLException {
                    return new GroupData(rs.getString("role"), rs.getString("wx"), rs.getString("num"), TimeUtil.currentDate());
                }
            });
            return sList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
